<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\Advertising;
use Hyperf\Database\Model\Builder;

/**
 * 广告DAO
 *
 * @author 谢声强(861167322@qq.com)
 * @package App\Dao
 */
class AdvertisingDao extends Dao
{
    use DaoTrait;

    /**
     * 通过type获取广告
     *
     * @param int $type 广告类型
     * @return mixed
     */
    public function getAdvertisingByType(int $type = 1)
    {
        return Advertising::query()->where('type', $type)->where('status', 1)->select('title', 'image', 'link')->orderByDesc('sort')->get();
    }

    /**
     * 通过type和地区获取广告
     * @param int $type
     * @param array $location_ids
     * @return mixed
     */
    public function getAdsByTypeAndLocations(int $type = 1, array $location_ids = [])
    {
        $model = Advertising::query()
            ->where('type', $type)
            ->where('status', 1)
        ->where(function (Builder $query)  {
            $query->where('start_time', '<=', time())->orWhere('start_time', null);
        } )
        ->where(function (Builder $query) {
            $query->where('end_time', '>=', time())->orWhere('end_time', null);
        });
        if (!empty($location_ids)) {
            $model = $model->where(function (Builder $query) use ($location_ids) {
                $query->whereIn('location_id', $location_ids)->orWhere('location_id', null);
            }); // 全国
        } else {
            $model = $model->where('location_id', null);
        }

        return $model->select('title', 'image', 'link', 'jump_type', 'jump_id')->orderByDesc('sort')->get();
    }

    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return Advertising::class;
    }

    /**
     * 根据$type,$location查询条件获取所有的广告信息
     * @param int $type 广告类型
     * @param mixed $location_id 广告投放位置
     * @param array $map 对应关系
     * @param string $sort 排序
     * @param array $select 选择字段
     * @param int $paginate 每页页数
     * @return mixed
     */
    public function getAdsList(int $type, $location_id = 0,array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();
        $type && $model = $model->where('type', $type);
        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }
        if ($location_id !== 0) {
            $model = $model->where('location_id', $location_id);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
       if (null !== $sort) {
           $model = $model->orderByRaw($sort);
       }

       if (null !== $select) {
           $model = $model->select($select);
       }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }

    /**
     * 通过ID获取广告状态
     * @param int $id
     * @return mixed
     */
    public function getStatusById(int $id)
    {
        return Advertising::query()->where('id', $id)->select('id', 'status')->first();
    }

    /**
     * 获取广告所有信息
     * @param int $id
     * @return mixed
     */
    public function getAllInfoById(int $id)
    {
        return Advertising::query()->where('id', $id)->first();
    }

}